/* Scripts for homepage


Show/Hide for About content on homepage */
var usernameRC;
var passwordRC;
var typeRC;
var aeRequest;
var actionType;

function createXmlHttpRequestObject() {
    var ro;
    var browser = navigator.appName;
    // Need to determine IE7 and not do this.
    if (browser == "Microsoft Internet Explorer") {
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else {
        ro = new XMLHttpRequest();
    }
    return ro;
}

function createResponseXML(textXML) {
    // code for IE
    if (window.ActiveXObject) {
        var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = "false";
        xmlDoc.loadXML(textXML);
        return xmlDoc;
    }
    // code for Mozilla, Firefox, Opera, etc.
    else {
        var parser = new DOMParser();
        var xmlDoc = parser.parseFromString(textXML, "text/xml");
        return xmlDoc;
    }
}

function loginUser(type,username,password){
    actionType = type;
    var url = '/RadioCabsProject-war/LoginServlet?type='+ type +'&username=' +username + '&password=' + password;
    //alert(url);
    aeRequest = createXmlHttpRequestObject();
    aeRequest.open("POST", url, true);
    aeRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    aeRequest.onreadystatechange = finishUpdate;
    aeRequest.send(null);
}

function logoutUser(){
    var url = '/RadioCabsProject-war/LogoutServlet';
    //alert(url);
    aeRequest = createXmlHttpRequestObject();
    aeRequest.open("POST", url, true);
    aeRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    aeRequest.onreadystatechange = finishUpdate;
    aeRequest.send(null);
}

function finishUpdate(){
    if(aeRequest.readyState==4){
        if(aeRequest.status == 200){
            var response = createResponseXML(aeRequest.responseText);
            var result = response.getElementsByTagName("result")[0].firstChild.nodeValue;
            if (result == "success") {
                if(actionType == "Driver" || actionType == "Listing"  || actionType == "Advertise"){
                    window.location.href= "home.jsp";
                }else if(actionType == "Admin"){
                    window.location.href = "../Admin/admin.jsp";
                }else{
                    window.location.href= "home.jsp";
                }
            }else{
                alert("Login failed, Please try again.");
            }
        }
    }
}

jQuery(document).ready(function() {
    jQuery(".showhidecontent").hide();
    //toggle the componenet with class msg_body
    jQuery(".showhideheading").click(function()
    {
        jQuery(".showhidecontent").slideToggle(500);
    });

    jQuery("#btnLogin").live('click',function(){
        usernameRC = jQuery("#txtUser").val();
        passwordRC = jQuery("#txtPass").val();
        typeRC = jQuery("#listUserType").val();

        loginUser(typeRC, usernameRC, passwordRC);
    });

    jQuery("#btnLogout").live('click',function(){
        logoutUser();
    });

    jQuery("#listUserType").change(function(){
       if(jQuery(this).val() == "Advertise"){
           //jQuery("#txtPass").fadeOut("slow");
           jQuery("#lbPass").fadeOut("slow");
       }else{
           //jQuery("#txtPass").fadeIn("fast");
           jQuery("#lbPass").fadeIn("slow");
       }
    });

    jQuery("#txtUser").keypress(function(event){
        if(event.which == 13){
            usernameRC = jQuery("#txtUser").val();
            passwordRC = jQuery("#txtPass").val();
            typeRC = jQuery("#listUserType").val();

            loginUser(typeRC, usernameRC, passwordRC);
        }
    })

    jQuery("#txtPass").keypress(function(event){
        if(event.which == 13){
            usernameRC = jQuery("#txtUser").val();
            passwordRC = jQuery("#txtPass").val();
            typeRC = jQuery("#listUserType").val();

            loginUser(typeRC, usernameRC, passwordRC);
        }
    })
});

// Spritely scripts for promo mouseovers

(function(jQuery) {
    jQuery(document).ready(function() {
        jQuery('#spritely4').sprite({fps: 2,  no_of_frames: 5}).spStop();
        jQuery('#spritely3').sprite({fps: 5, no_of_frames: 10}).spStop();
        jQuery('#spritely2').sprite({fps: 5, no_of_frames: 5}).spStop();
        jQuery('#spritely1').sprite({fps: 2, no_of_frames: 10}).spStop();

        //Begin mouseenter for #1
        jQuery('#spritely1').mouseenter(
            function() {jQuery('#spritely1').spStart();})
        //End mouseenter for #1 and begin mouseleave
        jQuery('#spritely1').mouseleave(
            function() {jQuery('#spritely1').spStop();})
        //End mouseleave for #1

        //Begin mouseenter for #2
        jQuery('#spritely2').mouseenter(
            function() {jQuery('#spritely2').spStart();})
        //End mouseenter for #2 and begin mouseleave
        jQuery('#spritely2').mouseleave(
            function() {jQuery('#spritely2').spStop();})
        //End mouseleave for #2

        //Begin mouseenter for #3
        jQuery('#spritely3').mouseenter(
            function() {jQuery('#spritely3').spStart();})
        //End mouseenter for #3 and begin mouseleave
        jQuery('#spritely3').mouseleave(
            function() {jQuery('#spritely3').spStop();})
        //End mouseleave for #3

        //Begin mouseenter for #4
        jQuery('#spritely4').mouseenter(
            function() {jQuery('#spritely4').spStart();})
        //End mouseenter for #4 and begin mouseleave
        jQuery('#spritely4').mouseleave(
            function() {jQuery('#spritely4').spStop();})
    //End mouseleave for #4
    });
})(jQuery);
// Spritely scripts for scolling banner on homepage

(function(jQuery) {
    jQuery(document).ready(function() {
        jQuery('#man')
        .sprite({fps: 10,no_of_frames: 15,
            // the following are optional: new in version 0.6...
            start_at_frame: 2,
            on_first_frame: function(obj) {
                if (window.console) {
            //                         console.log('first frame');
            }
            },
            on_last_frame: function(obj) {
                // you could stop the sprite here with, e.g.
                // obj.spStop();
                if (window.console) {
            //                       console.log('last frame');
            }
            },
            on_frame: {
                2: function(obj) {
                    // you could change the 'state' of the
                    // sprite here with, e.g. obj.spState(2);
                    if (window.console) {
                //                         console.log('frame 2');
                }
                }
            }
        })
        .spRandom({top: 220, bottom:250, left: 225, right: 250})
        .active();
        jQuery('#helicopter')
        .sprite({fps: 10,no_of_frames: 1,
            // the following are optional: new in version 0.6...
            start_at_frame: 2,
            on_first_frame: function(obj) {
                if (window.console) {
            //                         console.log('first frame');
            }
            },
            on_last_frame: function(obj) {
                // you could stop the sprite here with, e.g.
                // obj.spStop();
                if (window.console) {
            //                       console.log('last frame');
            }
            },
            on_frame: {
                2: function(obj) {
                    // you could change the 'state' of the
                    // sprite here with, e.g. obj.spState(2);
                    if (window.console) {
                //                         console.log('frame 2');
                }
                }
            }
        })
        .spRandom({top: 10,bottom:250, left: 10, right: 550,pause:3000})
        .active();

        jQuery('#clouds').pan({fps: 30, speed: 0.7,dir: 'left',  depth: 10});
        jQuery('#skyline3').pan({fps: 30, speed: 0.9, dir: 'left',depth: 12});
        jQuery('#buildings-bg').pan({fps: 30, speed: 1, dir: 'left', depth: 14});
        jQuery('#buildings-bg2').pan({fps: 30, speed: 1.5, dir: 'left', depth: 17});
        jQuery('#skyline').pan({fps: 30, speed: 1.2, dir: 'left',depth: 20});
        jQuery('#buildings').pan({fps: 30, speed: 2, dir: 'left', depth: 25});
        jQuery('#cars').pan({fps: 30, speed: 4.5, dir: 'left', depth: 23});
        jQuery('#buildings, #skyline3,#clouds, #buildings-bg, #buildings-bg2').spRelSpeed(7);
        window.page = {
            hide_panels: function() {
                jQuery('.panel').hide(300);
            },
            show_panel: function(el_id) {
                this.hide_panels();
                jQuery(el_id).show(300);
            }
        }
    });
})(jQuery);


//